import {
    Component,
    Injector,
    ViewEncapsulation,
    ViewChild,
    ElementRef
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    PipelineListDto,
    ProcViewDto,
    OrganizationUnitServiceProxy,
    EquipmentServiceProxy,
    EquipmentForNodeInput,
    NodeWorkerDto,
    NameValueDto,
    GetNodeWorkOutput,
    NodeEquipmentDto,
    ComboboxItemDto,
    SetNodeWorkersInput,
    SetNodeEquipmentsInput
} from '@shared/service-proxies/service-proxies';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import {
    PipelineNodesServiceProxy,
    StationDto
} from '@shared/service-proxies/service-proxies';
import { SelectItem } from 'primeng/api';

@Component({
    selector: 'gxzx',
    templateUrl: './gxzx.component.html',
    styleUrls: ['./gxzx.component.less'],
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class GXZXModalComponent extends AppComponentBase {
    @ViewChild('stationCombobox') stationCombobox: ElementRef;

    stationId = 0;
    station = new StationDto();
    stations = new Array<StationDto>();
    equipments = new Array<NodeEquipmentDto>();
    shebei: ComboboxItemDto[];
    selectshebei: ComboboxItemDto[];
    selectsb = new Array<EquipmentForNodeInput>();
    gongzhuang: ComboboxItemDto[];
    selectgongzhuang: ComboboxItemDto[];

    nodeWorker = new Array<GetNodeWorkOutput>();
    newCar: boolean;
    saving = false;
    displayDialog: boolean;

    users = new Array<NameValueDto>();
    //user: NameValueDto;
    setNodeWorkers = new SetNodeWorkersInput();
    setNodeEquipments = new SetNodeEquipmentsInput();
    pipe: PipelineListDto = new PipelineListDto();

    procInfo = new ProcViewDto();

    constructor(
        injector: Injector,
        private _taskServiceProxy: PipelineNodesServiceProxy,
        private _equipmentServiceProxy: EquipmentServiceProxy,
        private _organizationUnitServiceProxy: OrganizationUnitServiceProxy
    ) {
        super(injector);
    }

    load(pipe: PipelineListDto): void {
        this.pipe = pipe;
        this.nodeWorker = new Array<GetNodeWorkOutput>();
        this.reloadProcInfo();
        this._taskServiceProxy
            .getNodeEquipments(pipe.nodeId)
            .subscribe(result => {
                this.equipments = result.items;
                this.selectshebei = new Array<ComboboxItemDto>();
                result.items.forEach(item => {
                    let com = new ComboboxItemDto();
                    com.displayText = item.displayName ;
                    com.value = item.sysId;

                    this.selectshebei.push(com);
                });
            });
        this._equipmentServiceProxy
            .getEquipmentForPipeNode()
            .subscribe(result => {
                this.selectsb = result;
                this.shebei = new Array<ComboboxItemDto>();
                result.forEach(item => {
                    let com = new ComboboxItemDto();
                    com.displayText = item.equipName ;
                    com.value = item.id.toString();
                    this.shebei.push(com);
                });
            });
        this._taskServiceProxy.getNodeWorkers(pipe.nodeId).subscribe(result => {
            this.nodeWorker = result;

        });
        this._organizationUnitServiceProxy
            .findUsersForNode()
            .subscribe(result => {
                this.users = result;
            });
    }
    selectgw() {
        this.stations.forEach(el => {
            if (this.stationId === el.id) {
                this.station = el;
            }
        });
    }
    start() {

        this._taskServiceProxy
            .componentProcStart(this.pipe.id)
            .subscribe(result => {
                this.notify.success('工序开始成功');
                this.reloadProcInfo();
            });
    }

    pause() {
        this._taskServiceProxy
            .componentProcPause(this.pipe.id)
            .subscribe(result => {
                this.notify.success('工序暂停成功');
                this.reloadProcInfo();
            });
    }

    finish() {
        this.addOther();
        this._taskServiceProxy
            .componentProcFinish(this.pipe.id)
            .subscribe(result => {
                this.notify.success('工序完成成功');
                this.reloadProcInfo();
            });
    }

    restart() {
        this._taskServiceProxy
            .componentProcReInit(this.pipe.id)
            .subscribe(result => {
                this.notify.success('工序重置成功');
                this.reloadProcInfo();
            });
    }

    reloadProcInfo() {
        this._taskServiceProxy.getProcInfo(this.pipe.id).subscribe(result => {
            this.procInfo = result;
        });
    }

    showDialogToAdd() {
        let selected = new GetNodeWorkOutput();
        selected.userName = new NameValueDto();
        selected.userName = this.users[0];
        selected.timePercentage = 100;
        this.nodeWorker.push(selected);

    }
    addOther() {
        let nodeWorkers = new Array<NodeWorkerDto>();
        this.nodeWorker.forEach(item => {
            let nodeWorker = new NodeWorkerDto();
            nodeWorker.userId = Number(item.userName.value);
            nodeWorker.userName = item.userName.name;
            nodeWorker.timePercentage = item.timePercentage;
            nodeWorker.id = item.id;
            nodeWorker.nodeEntityId = this.pipe.nodeId;
            nodeWorker.pipelineEntityId = this.pipe.id;
            nodeWorkers.push(nodeWorker);
        });
        this.setNodeWorkers.workers = nodeWorkers;
        this.setNodeWorkers.nodeId = this.pipe.nodeId;
        this.setNodeWorkers.pipelineId = this.pipe.id;
        this._taskServiceProxy
            .setNodeWorkers(this.setNodeWorkers)
            .subscribe(result => { });
        //////////
        //////////
        let nodeEquipments = new Array<NodeEquipmentDto>();
        this.selectshebei.forEach(item => {
            let nodeEquipment = new NodeEquipmentDto();
            nodeEquipment.sysId = item.value;
            nodeEquipment.nodeEntityId = this.pipe.nodeId;
            nodeEquipment.pipelineEntityId = this.pipe.id;
            nodeEquipment.displayName = item.displayText;

            nodeEquipment.code = this.selectsb.find(p => p.equipName === item.displayText).code; // item.displayText.split('&')[1];
            console.log(nodeEquipment);
            this.equipments.forEach(eq => {
                if (
                    eq.code === nodeEquipment.code &&
                    eq.displayName === nodeEquipment.displayName
                ) {
                    nodeEquipment.id = eq.id;
                }
            });
            nodeEquipments.push(nodeEquipment);
        });
        this.setNodeEquipments.equipments = nodeEquipments;
        this.setNodeEquipments.nodeId = this.pipe.nodeId;
        this.setNodeEquipments.pipelineId = this.pipe.id;
        this._taskServiceProxy
            .setNodeEquipments(this.setNodeEquipments)
            .subscribe(result => { });
    }
}
